package com.unsafe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class ListTest {

    public static void main(String[] args) {
        //让ArrayList变得线程安全
        /**
         * 1.用Vector
         * 2. List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
         * juc下面的 List<Integer> list = new CopyOnWriteArrayList<Integer>();  //写入时复制
         *
         */
        List<Integer> list = new CopyOnWriteArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            new Thread(()->{
                list.add(finalI);
                System.out.println(list);
            },"i").start();
            System.out.println(list);
        }

        System.out.println("长度" + list.size());



    }
}
